package com.zys.sac.core.helper;

import lombok.Data;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author junekzhong
 * @date 2023/12/25 17:53
 */
@Data
public class UrlRule {

    public UrlRule(String url, String rule) {
        this.url = url;
        this.rule = rule;
        antPathRequestMatchers.add(new AntPathRequestMatcher(url, HttpMethod.GET.name()));
        antPathRequestMatchers.add(new AntPathRequestMatcher(url, HttpMethod.POST.name()));
        antPathRequestMatchers.add(new AntPathRequestMatcher(url, HttpMethod.OPTIONS.name()));
        antPathRequestMatchers.add(new AntPathRequestMatcher(url, HttpMethod.DELETE.name()));
        antPathRequestMatchers.add(new AntPathRequestMatcher(url, HttpMethod.PUT.name()));
    }

    public boolean match(HttpServletRequest request) {
        for (AntPathRequestMatcher antPathRequestMatcher : antPathRequestMatchers) {
            if (antPathRequestMatcher.matches(request)) {
                return true;
            }
        }
        return false;
    }

    private String url;

    private List<AntPathRequestMatcher> antPathRequestMatchers = new ArrayList<>();

    private String rule;


}
